import java.util.*;

public class Text {
    //566
    public int[][] matrixReshape(int[][] mat, int r, int c) {
        int m = mat.length;
        int n = mat[0].length;
        if(m * n != r * c) {
            return mat;
        }
        int[][] ret = new int[r][c];
        for(int i = 0; i < m * n; i++) {
            ret[i/c][i%c] = mat[i/n][i%n];
        }
        return ret;
    }
    //118
    public List<List<Integer>> generate(int numRows) {
        List<Integer> tmp = new ArrayList<>();
        List<List<Integer>> ret = new ArrayList<>();
        tmp.add(1);
        ret.add(tmp);
        for(int i = 1; i < numRows; i++) {
            List<Integer> prev = ret.get(i-1);
            List<Integer> cur = new ArrayList<>();
            cur.add(1);
            for(int j = 1; j < i; j++) {
                cur.add(prev.get(j)+prev.get(j-1));
            }
            cur.add(1);
            ret.add(cur);
        }
        return ret;
    }
    public String printBin(double num) {
        StringBuilder str = new StringBuilder("0.");
        for(int i = 0; i < 6; i++) {
            num = num*2;
            int tmp = (int)num;
            if(tmp == 1) {
                str.append('1');
            }else {
                str.append('0');
            }
            num -= tmp;
            if(num == 0) {
                return str.toString();
            }
        }
        return "ERROR";
    }
    List<List<Integer>> ret = new ArrayList<>();
    List<Integer> tmp = new LinkedList<>();
    public void func(int[] nums,int[] used) {
        if(tmp.size() == nums.length) {
            ret.add(new ArrayList<>(tmp));
            return;
        }
        for(int i = 0; i < nums.length; i++) {
            if(used[i] == 1) {
                continue;
            }
            tmp.add(nums[i]);
            used[i] = 1;
            func(nums,used);
            used[i] = 0;
            tmp.remove(tmp.size()-1);
        }
    }
    public List<List<Integer>> permute(int[] nums) {
        int[] used = new int[nums.length];
        func(nums,used);
        return ret;
    }
    //47
    /*List<List<Integer>> ret = new ArrayList<>();
    List<Integer> tmp = new LinkedList<>();

    public void func(int[] nums,int[] used) {
        if(tmp.size() == nums.length) {
            ret.add(new ArrayList<>(tmp));
            return;
        }
        for(int i = 0; i < nums.length; i++) {
            if(i > 0 && nums[i] == nums[i-1] && used[i-1] == 0) {
                continue;
            }
            if(used[i] == 1) {
                continue;
            }
            tmp.add(nums[i]);
            used[i] = 1;
            func(nums,used);
            used[i] = 0;
            tmp.remove(tmp.size()-1);
        }
    }
    public List<List<Integer>> permuteUnique(int[] nums) {
        int[] used = new int[nums.length];
        Arrays.sort(nums);
        func(nums,used);
        return ret;
    }*/
}
